package com.geeksaga.web;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.geeksaga.common.util.StringUtils;
import com.geeksaga.domain.Bbs;
import com.geeksaga.search.LuceneEngine;
import com.geeksaga.service.BbsService;

/**
 * @author newbie
 * @since 0.1
 */
@Controller
public class SearchController {

    protected static final Log logger = LogFactory.getLog(SearchController.class);
    
	@Autowired BbsService bbsService = null;

	@RequestMapping(value = { "/bbs/search.do" })
	public void search(@RequestParam(required = false) String keyword, ModelMap model) {
		if (!StringUtils.isEmpty(keyword)) {
			List<String> keyList = LuceneEngine.getInstance().search(keyword);
			
			if(keyList != null && keyList.size() > 0)
				model.addAttribute("list", bbsService.search(keyList));
			else
				model.addAttribute("list", new ArrayList<Bbs>());
		}
	}
}